package main.database;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import main.GlobalConfig;
import main.TimeHelper;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.List;

public class CollectionHelper {
    public static boolean toggleUserCollect(String uuid, String id) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfUserCollect);

        MongoCursor<Document> cursor = CollectionHelper.getSearchCursor(uuid, id);
        boolean collected = true;
        if (cursor.hasNext()) {
            Document doc = cursor.next();
            boolean oldCollectValue = (boolean) doc.get("collect");
            boolean newCollectValue = !oldCollectValue;

            Bson filter = new Document("uuid", uuid).append("fictionid", id);
            Bson newValue = new Document("collect", newCollectValue);
            Bson updateOperationDocument = new Document("$set", newValue);
            collection.updateOne(filter, updateOperationDocument);

            collected = newCollectValue;
        }
        else {
            String time = TimeHelper.getFormattedNowString();
            Document document = new Document("uuid", uuid)
                    .append("fictionid", id)
                    .append("collect", true)
                    .append("time", time);
            collection.insertOne(document);
        }
        cursor.close();

        return collected;
    }

    public static boolean checkUserCollection(String uuid, String id) {
        MongoCursor<Document> cursor = CollectionHelper.getSearchCursor(uuid, id);
        boolean collected = false;
        if (cursor.hasNext()) {
           collected = (boolean) cursor.next().get("collect");
        }
        cursor.close();

        return collected;
    }

    public static List<String[]> getUserCollectionInfos(String uuid) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfUserCollect);
        Bson filter1 = Filters.eq("uuid", uuid);
        Bson filter2 = Filters.eq("collect", true);
        Bson filters = Filters.and(filter1, filter2);

        FindIterable<Document> documents = collection.find(filters);
        MongoCursor<Document> cursor = documents.iterator();

        List<String[]> resultList = new ArrayList<>();
        while(cursor.hasNext()) {
            String[] result = new String[5];
            Document doc = cursor.next();

            String articleId = doc.get("fictionid").toString();
            String[] articleInfos = ArticleHelper.getFieldsFromId2(articleId, false);
            result[0] = articleInfos[0]; //article title
            result[1] = AuthorHelper.getAuthorNameFromId(articleInfos[1]); //article author
            result[2] = articleInfos[2]; //article score
            result[3] = doc.get("time").toString(); //collect time
            result[4] = articleId; // article id

            resultList.add(result);
        }
        cursor.close();

        return resultList;
    }

    private static MongoCursor<Document> getSearchCursor(String uuid, String id) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfUserCollect);
        Bson uuidFilter = Filters.eq("uuid", uuid);
        Bson fictionFilter = Filters.eq("fictionid", id);
        Bson filter = Filters.and(uuidFilter, fictionFilter);
        FindIterable<Document> findIterable = collection.find(filter);
//        AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
//                new Document("$match", new Document("uuid", uuid)),
//                new Document("$match", new Document("fictionid", id))
//        ));

        return findIterable.iterator();
    }
}
